<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>仓库WMS</title>
    <link rel="shortcut icon" href="favicon.ico">
    <script src="../../res/js/comm.js"></script>
    <link href="../../res../res/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
    <script src="../../js/wms/select.js"></script>
    <script src="../../res/js/ajaxUtil.js"></script>
</head>
<body class="gray-bg">

<style>
    .select2-container--open {
        z-index: 10000000000
    }
</style>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="panel panel-info">
        <div class="panel-heading"><i class="fa fa-info-circle"></i> 仓库列表</div>
        <div class="panel-body">

            <div class="ibox-query">
                <div class="row">
                    <form id="wmsForm" method="post" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-1 control-label">仓库编码</label>
                            <div class="col-sm-2">
                                <input id="warehouseCode" name="warehouseCode" class="form-control" type="text">
                            </div>
                            <label class="col-sm-1 control-label">仓库名称</label>
                            <div class="col-sm-2">
                                <input id="name" name="name" class="form-control" type="text">
                            </div>

                            <div>
                                <button style="margin-left: 100px;" class="btn btn-info" onclick='$.openx.query()'
                                        type="button"><i class="glyphicon glyphicon-search"></i> 查询
                                </button>
                            </div>
                            <!--<label class="col-sm-1 control-label">应用URL</label>-->
                            <!--<div class="col-sm-2">-->
                            <!--<input name="" class="form-control" placeholder="请输入文本" type="text">-->
                            <!--</div>-->
                            <!--<label class="col-sm-1 control-label">创建时间</label>-->
                            <!--<div class="col-sm-2">-->
                            <!--<input name="" class="form-control" placeholder="请输入文本" type="text">-->
                            <!--</div>-->
                        </div>
                        <div class="hr-line-dashed"></div>
                    </form>
                </div>
            </div>

            <div class="row row-lg">
                <!--<div class="col-sm-12">-->
                    <!--<div id="WMSBar">-->
                <!--</div>-->
                <div class="bars pull-left">
                    <button class="btn btn-warning" onclick='$.openx.insert()' type="button"><i
                            class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <button class="btn btn-primary" onclick='$.openx.update()' type="button"><i
                            class="glyphicon glyphicon-pencil"></i> 修改
                    </button>
                    <button class="btn btn-danger" onclick='$.openx.delete()' type="button"><i
                            class="glyphicon glyphicon-trash"></i> 删除
                    </button>
                    <button class="btn btn-info" onclick='push2Logistics()' type="button"> 同步仓库数据</button>
                </div>
                <table id="table">
                    <thead>
                    <tr>

                        <th data-align="center" data-field="_checkbox_status" data-checkbox="true"></th>
                        <th data-align="center" data-formatter="$.openx.generatorId"> 序号</th>
                        <th data-align="center" data-field="warehouseCode" data-formatter="codeFormatter">仓库编码</th>
                        <th data-align="center" data-field="name">仓库名称</th>
                        <th data-align="center" data-field="type" data-formatter="warehouseTypeFormatter">仓库类型</th>
                        <!--<th data-align="center" data-field="address">仓库地址</th>-->
                        <!--<th data-align="center" data-field="phone">联系人电话</th>-->
                        <!--<th data-align="center" data-field="longitude">经度</th>-->
                        <!--<th data-align="center" data-field="latitude">纬度</th>-->
                        <th data-align="center" data-field="status" data-formatter="warehouseStatusFormatter">仓库状态</th>
                        <th data-align="center" data-field="phone">仓库电话</th>
                        <th data-align="center" data-field="deliveryTime">发货时间</th>
                        <!--<th data-align="center" data-field="remark">仓库描述</th>-->

                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
</div>
</body>


<script type="text/html" id="wmsDialog">
    <form class="form-horizontal" id="wmsPartnerForm">

        <div class="col-md-12" style="width: 800px;margin-top: 70px">
            <div class="form-group">
                <label class="col-sm-4 control-label"><font color="red">* </font>仓库编码：</label>
                <div class="col-sm-8">
                    <input name="warehouseCode" id="warehouseCodeInsert" class="form-control" placeholder="请输入仓库编码"
                           type="text" title="必填字段" required="true" value="{{!d.warehouseCode ? '':d.warehouseCode}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label"><font color="red">* </font>仓库名称：</label>
                <div class="col-sm-8">
                    <input name="name" id="warehouseName" class="form-control" placeholder="请输入仓库名称" type="text"
                           title="必填字段" required="true" value="{{!d.name ? '':d.name}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">仓库类型：</label>
                <div class="col-sm-8">
                    <div class="radio radio-info radio-inline">
                        <input type="radio" name="type" id="inlineRadio1" value="SHOP"
                               checked="{{d.type == 'SHOP' ? true : false}}"/>
                        <label for="inlineRadio1"> 门店 </label>
                    </div>
                    <div class="radio radio-info radio-inline">
                        <input type="radio" name="type" id="inlineRadio2" value="MAIN"
                               {checked="{{d.type == 'MAIN' ? true : false}}"/>
                        <label for="inlineRadio2"> 大仓 </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">仓库地址：</label>
                <div class="col-sm-8">
                    <input name="address" id="wmsAddress" class="form-control" placeholder="请输入仓库地址" type="text"
                           value="{{!d.address ? '':d.address}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-4 control-label"><i class="snowflake"></i>所属城市：</label>
                <div class="col-sm-8">
                    <input name="updateRegionCode" id="updateRegionCode" class="form-control" type="hidden"
                           value="{{!d.regionCode ? '':d.regionCode}}">
                    <select id="province" style="width: 150px;">
                        <option value="">请选择省/直辖市</option>
                    </select>
                    <select id="city" style="width: 150px; ">
                        <option value="">请选择市</option>
                    </select>
                    <select id="regionCode" name="regionCode" style="width: 150px; ">
                        <option value="">请选择区/县</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">联系人电话：</label>
                <div class="col-sm-8">
                    <input name="phone" id="wmsPhone" class="form-control" placeholder="请输入联系人电话" type="text"
                           value="{{!d.phone ? '':d.phone}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">经度：</label>
                <div class="col-sm-8">
                    <input name="longitude" class="form-control" placeholder="请输入经度" type="text"
                           value="{{!d.longitude ? '':d.longitude}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">纬度：</label>
                <div class="col-sm-8">
                    <input name="latitude" class="form-control" placeholder="请输入纬度" type="text"
                           value="{{!d.latitude ? '':d.latitude}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">仓库状态：</label>
                <div class="col-sm-8">
                    <div class="radio radio-info radio-inline">
                        <input type="radio" name="status" id="statusRadio1" value="OPEN"
                               checked="{{d.status == 'OPEN' ? true : false}}"/>
                        <label for="statusRadio1"> 营业 </label>
                    </div>
                    <div class="radio radio-info radio-inline">
                        <input type="radio" name="status" id="statusRadio2" value="CLOSE"
                               {checked="{{d.status == 'CLOSE' ? true : false}}"/>
                        <label for="statusRadio2"> 停业 </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">发货时间：</label>
                <select id="inp_h" style="margin-left:16px;margin-top:8px;"> </select>
                --
                <select id="end_h" style="margin-top:8px;"> </select>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">是否自动下账：</label>
                <div class="col-sm-8">
                    <input id="autoPosting" name="autoPosting" type="checkbox"
                           {{# if(d.autoPosting){ }}
                             checked
                           {{# } }}
                    >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">智能协调是否自动下账：</label>
                <div class="col-sm-8">
                    <input id="consortAutoPosting" name="consortAutoPosting" type="checkbox"
                           {{# if(d.consortAutoPosting){ }}
                           checked
                           {{# } }}
                    >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">是否支持即时配送：</label>
                <div class="col-sm-8">
                    <input id="supportRealTime" name="supportRealTime" type="checkbox"
                           {{# if(d.supportRealTime){ }}
                           checked
                           {{# } }}
                    >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">是否自动退货入库：</label>
                <div class="col-sm-8">
                    <input id="autoAfs" name="autoAfs" type="checkbox"
                           {{# if(d.autoAfs){ }}
                           checked
                           {{# } }}
                    >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">仓库描述：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" placeholder="请输入仓库描述" type="text"
                           value="{{!d.remark ? '':d.remark}}">
                </div>
            </div>
        </div>
    </form>
</script>
<script type="text/html" id="warehouseDetail">
    <div style="padding: 10px">
        <table class="layui-table">
            <tbody>
            <tr>
                <td class="layui-table-td-bg">仓库编码</td>
                <td class="text-center">{{d.warehouseCode ? d.warehouseCode :"" }}</td>
                <td class="layui-table-td-bg">仓库名称</td>
                <td class="text-center">{{d.name ? d.name :"" }}</td>
            </tr>
            <tr>
                <td class="layui-table-td-bg">仓库类型</td>
                <td class="text-center" data-formatter="warehouseTypeFormatter">{{d.type ? d.type :"" }}</td>
                <td class="layui-table-td-bg">描述</td>
                <td class="text-center">{{d.remark ? d.remark :"" }}</td>
            </tr>
            <tr>
                <td class="layui-table-td-bg">仓库地址</td>
                <td class="text-center">{{d.address ? d.address :"" }}</td>
                <td class="layui-table-td-bg">区域编码</td>
                <td class="text-center">{{d.regionCode ? d.regionCode :"" }}</td>
            </tr>
            <tr>
                <td class="layui-table-td-bg">经度</td>
                <td class="text-center">{{d.longitude ? d.longitude :"" }}</td>
                <td class="layui-table-td-bg">纬度</td>
                <td class="text-center">{{d.latitude ? d.latitude :"" }}</td>
            </tr>
            <tr>
                <td class="layui-table-td-bg">电话</td>
                <td class="text-center">{{d.phone ? d.phone :"" }}</td>
                <td class="layui-table-td-bg">状态</td>
                <td class="text-center" data-formatter="warehouseStatusFormatter">{{d.status ? d.status :"" }}</td>
            </tr>
            </tbody>
        </table>
    </div>
</script>


<script>


    var ownOptions = {};
    /**查询参数信息*/
    var url = "/openx/wms/physicalWMSService/queryWarehouseForPage";
    //对应接口入参
    var param = {};

    var formId = 'wmsForm';

    (function () {
        /**设置增删改查的工具条*/
        $.openx.setTableToolBar('WMSBar');
        /**进行表格数据初始化*/
        var data = $.openx.getJsonObj(formId);
        param.bean = data;
        $.openx.initTable("table", url, param);
        //小时和分钟的初始值
    })();

    function codeFormatter(value, row) {
        return "<a style = '\color:#000000;text-decoration:underline;\' onclick='javascript:queryDetail(" + JSON.stringify(row) + ");'><b>" + row.warehouseCode + "</b></a>";
    }


    function warehouseTypeFormatter(value, row) {
        if (value === 'SHOP') {
            return '<font >门店</font>'
        } else if (value === 'MAIN') {
            return '<font >大仓</font>'
        }
        return '';
    }

    function warehouseStatusFormatter(value, row) {
        if (value === 'CLOSE') {
            return '<font color="red">停业</font>'
        } else if (value === 'OPEN') {
            return '<font color="green">营业</font>'
        }
        return '';
    }

    function queryDetail(row) {
        $X("/wms/physicalWMSService", "queryDetail").callx({
            warehouseCode: row.warehouseCode,
            onResult: function (data) {
                if (data) {
                    if (data.status === 'CLOSE') {
                        data.status = '停业';
                    } else if (data.status === 'OPEN') {
                        data.status = '营业';
                    }
                    if (data.type === 'SHOP') {
                        data.type = '门店';
                    } else if (data.type === 'MAIN') {
                        data.type = '大仓';
                    }
                    var innerHtml = $.openx.innerHtml('warehouseDetail');
                    laytpl(innerHtml).render(data, function (html) {
                        $.openx.openLayer(['auto', 'auto'], '查询仓库详情', html, true);
                    });
                } else {
                    layer.msg("无详情信息")
                }
            },
            onError: function (error) {
                var res = JSON.parse(error);
                layer.msg(res.message)
            }

        });
    }


    $.openx.insert = function () {
        var createForm = "wmsPartnerForm";
        var innerHtml = $.openx.innerHtml('wmsDialog');
        laytpl(innerHtml).render({}, function (html) {
            $.openx.openLayer(['1000', '750'], '新增实体仓库', html, false, function () {
                if ($.openx.validation(createForm)) {
                    //获取表单数据
                    var param = $.openx.getJsonObj(createForm);
                    if (getDistrictCode("regionCode") == '') {
                        if (getDistrictCode("city") == '') {
                            if (getDistrictCode("province") == '') {
                                param.regionCode = '';
                            } else {
                                param.regionCode = getDistrictCode("province");
                            }
                        } else {
                            param.regionCode = getDistrictCode("city");
                        }
                    } else {
                        param.regionCode = getDistrictCode("regionCode");
                    }

                    var startHour = $('#inp_h').find("option:selected").text().split(':')[0];
                    var endHour = $('#end_h').find("option:selected").text().split(':')[0];
                    if (parseInt(startHour) > parseInt(endHour)) {
                        layer.msg("发货时间:开始时间必须小于结束时间!");
                        return;
                    }

                    param.deliveryTime = $('#inp_h').find("option:selected").text() + "-" + $('#end_h').find("option:selected").text();
                    param.autoPosting = $("#autoPosting").is(':checked');
                    param.consortAutoPosting = $("#consortAutoPosting").is(':checked');
                    param.supportRealTime = $("#supportRealTime").is(':checked');

                    $X("/wms/physicalWMSService", "createWarehouse").callx({
                        param: param,
                        onResult: function (data) {
                            if (data) {
                                $.openx.tableRefresh(null, null, true);
                                layer.closeAll();
                                layer.msg("新增成功");
                            }
                        },
                        onError: function (error) {
                            var res = JSON.parse(error);
                            layer.msg(res.message)
                        }
                    });
                }
            });
        });
        initAreaSelect("province", "city", "regionCode");

        deliveryTime();
    }

    $.openx.update = function () {
        $.openx.onClickUpdate(function (rows) {
            var areaData = queryAreaDetailByAreaCode(rows[0].regionCode);
            var updateForm = "wmsPartnerForm";
            var innerHtml = $.openx.innerHtml('wmsDialog');
            laytpl(innerHtml).render(rows[0], function (html) {
                $.openx.openLayer(['1000', '750'], '修改实体仓库', html, false, function () {
                    if ($.openx.validation(updateForm)) {
                        //获取表单数据
                        var param = $.openx.getJsonObj(updateForm);
                        if (getDistrictCode("regionCode") == '') {
                            if (getDistrictCode("city") == '') {
                                if (getDistrictCode("province") == '') {
                                    param.regionCode = $('#updateRegionCode').val();
                                } else {
                                    param.regionCode = getDistrictCode("province");
                                }
                            } else {
                                param.regionCode = getDistrictCode("city");
                            }
                        } else {
                            param.regionCode = getDistrictCode("regionCode");
                        }
                        param.id = rows[0].id;

                        var startHour = $('#inp_h').find("option:selected").text().split(':')[0];
                        var endHour = $('#end_h').find("option:selected").text().split(':')[0];
                        var startMinute = $('#inp_h').find("option:selected").text().split(':')[1];
                        var endMinute = $('#end_h').find("option:selected").text().split(':')[1];
                        if (parseInt(startHour) > parseInt(endHour)) {
                            layer.msg("发货时间:开始时间必须小于结束时间!");
                            return;
                        }
                        if (parseInt(startHour) == parseInt(endHour)) {
                            if (parseInt(startMinute) > parseInt(endMinute)) {
                                layer.msg("发货时间:开始时间必须小于结束时间!");
                                return;
                            }
                        }

                        param.deliveryTime = $('#inp_h').find("option:selected").text() + "-" + $('#end_h').find("option:selected").text();
                        param.autoPosting = $("#autoPosting").is(':checked');
                        param.consortAutoPosting = $("#consortAutoPosting").is(':checked');
                        param.supportRealTime = $("#supportRealTime").is(':checked');
                        param.autoAfs = $("#autoAfs").is(':checked');
                        $X("/wms/physicalWMSService", "updateWarehouse").callx({
                            param: param,
                            onResult: function (data) {
                                if (data) {
                                    $.openx.tableRefresh(null, null, true);
                                    layer.closeAll();
                                    layer.msg("修改成功");
                                }
                            },
                            onError: function (error) {
                                var res = JSON.parse(error);
                                layer.msg(res.message)
                            }
                        });
                    }
                });
            });

            var h = 0, m = 0;
            var inp_h = document.getElementById("inp_h");
            var end_h = document.getElementById("end_h");

            console.log(inp_h.options)
            //清空小时和分钟已有的选项
            for (var i = 0; i < inp_h.options.length;) {
                inp_h.remove(inp_h.options[i]);
                end_h.remove(end_h.options[i]);
            }

            for (h; h < 24; h++) {
                var opt = document.createElement("option");
                opt.text = h < 10 ? '0' + h + ':00' : h + ':00';
                inp_h.add(opt);
                var opt2 = document.createElement("option");
                opt2.text = h < 10 ? '0' + h + ':30' : h + ':30';
                inp_h.add(opt2);
                var opt3 = document.createElement("option");
                opt3.text = h < 10 ? '0' + h + ':00' : h + ':00';
                end_h.add(opt3);
                var opt4 = document.createElement("option");
                opt4.text = h < 10 ? '0' + h + ':30' : h + ':30';
                end_h.add(opt4);
            }
            if (rows[0].deliveryTime !== undefined) {
                var deliveryTimes = rows[0].deliveryTime.split("-");
                console.log(deliveryTimes);

                var options = document.getElementById('inp_h').children;
                for (var j = 0; j < options.length; j++) {
                    if (options[j].text === deliveryTimes[0]) {
                        options[j].selected = true;
                    }
                }
                var options2 = document.getElementById('end_h').children;
                for (var k = 0; k < options.length; k++) {
                    if (options2[k].text === deliveryTimes[1]) {
                        options2[k].selected = true;
                    }
                }
            } else {
                var options = document.getElementById('inp_h').children;
                options[16].selected = true;
                var options = document.getElementById('end_h').children;
                options[42].selected = true;
            }

            initAreaSelect("province", "city", "regionCode");
            if (rows[0].type == 'MAIN') {
                $('#inlineRadio2').attr('checked', true);
            } else if (rows[0].type == 'SHOP') {
                $('#inlineRadio1').attr('checked', true);
            }
            if (rows[0].status == 'OPEN') {
                $('#statusRadio1').attr('checked', true);
            } else if (rows[0].status == 'CLOSE') {
                $('#statusRadio2').attr('checked', true);
            }
            $('#province option').filter(function () {
                return $(this).html() == areaData.province;
            }).attr("selected", true);
            $('#province').trigger("change");
            $('#city option').filter(function () {
                return $(this).html() == areaData.city;
            }).attr("selected", true);

            $('#city').trigger("change");
            $('#regionCode option').filter(function () {
                return $(this).html() == areaData.district;
            }).attr("selected", true);

        });
    }

    function push2Logistics() {
        if ($.openx.rows.length <= 0) {
            layer.msg("请选择需要同步的仓库", {time: 2000});
            return false;
        } else {
            $.openx.confirm('确认要进行同步操作', function () {
                var rows = $.openx.getSelectedRow();
                var warehouseCodes = [];
                for (var i = 0; i < rows.length; i++) {
                    warehouseCodes.push(rows[i].warehouseCode);
                }
                $X("/wms/physicalWMSService", "push2Logistics").callx({
                    warehouseCodes: warehouseCodes,
                    onResult: function (data) {
                        layer.closeAll();
                        layer.msg("推送成功");
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });
            })

        }

     /*   $.openx.onClickDelete(function (rows) {
            var ids = new Array();
            for (var i = 0; i < rows.length; i++) {
                ids.push(rows[i].id);
            }
            $X("/boss/channelGoodsService", "deleteChannelGoods").callx({
                id: ids,
                onResult: function (result) {
                    layer.closeAll();
                    refreshTable();
                },
                onError: function (error) {
                }
            });
        });*/
    }


    function deliveryTime() {
        var h = 0, m = 0;
        var inp_h = document.getElementById("inp_h");
        var end_h = document.getElementById("end_h");

        console.log(inp_h.options)
        //清空小时和分钟已有的选项
        for (var i = 0; i < inp_h.options.length;) {
            inp_h.remove(inp_h.options[i]);
            end_h.remove(end_h.options[i]);
        }

        for (h; h < 24; h++) {
            var opt = document.createElement("option");
            opt.text = h < 10 ? '0' + h + ':00' : h + ':00';
            inp_h.add(opt);
            var opt2 = document.createElement("option");
            opt2.text = h < 10 ? '0' + h + ':30' : h + ':30';
            inp_h.add(opt2);
            var opt3 = document.createElement("option");
            opt3.text = h < 10 ? '0' + h + ':00' : h + ':00';
            end_h.add(opt3);
            var opt4 = document.createElement("option");
            opt4.text = h < 10 ? '0' + h + ':30' : h + ':30';
            end_h.add(opt4);
        }

        var options = document.getElementById('inp_h').children;
        options[16].selected = true;
        var options = document.getElementById('end_h').children;
        options[42].selected = true;
    }


    $.openx.delete = function () {
        $.openx.onClickDelete(function (rows) {
            console.log(rows[0].warehouseCode)
            $X("/wms/physicalWMSService", "deleteWarehouse").callx({
                warehouseCode: rows[0].warehouseCode,
                onResult: function (data) {
                    if (data) {
                        $.openx.tableRefresh(null, null, true);
                        layer.closeAll();
                        layer.msg("删除成功");
                    }
                },
                onError: function (error) {
                    var res = JSON.parse(error);
                    layer.msg(res.message)
                }
            });
        })
    }

    $.openx.query = function () {
        var data = $.openx.getJsonObj(formId);
        param.bean = data;
        $.openx.tableRefresh(url, param);
    }


    function get_hm() {

    }

</script>


</html>
